<html>
<head>
<title>Original Title</title>
<link rel="shortcut icon" type="image/x-icon" href="http://test.com/oldfavicon.ico"/>
<script>
if (window.testRunner)
    testRunner.waitUntilDone();

function debugOutput(str) {
    text = document.createTextNode(str);
    debugDiv = document.getElementById('debugDiv');
    div = document.createElement ('div');
    div.appendChild(text);
    debugDiv.appendChild(div);
}

function createFavIconElement(iconURL) {
    var link = document.createElement("link");
    link.type = "image/x-icon";
    link.rel = "shortcut icon";
    link.href = iconURL;
    return link;
}

function getHeadElement() {
    return document.getElementsByTagName("head")[0];
}

function setFavIcon(iconURL) {
    var docHead = getHeadElement();
    var links = docHead.getElementsByTagName("link");
    for (var i = 0; i < links.length; ++i) {
      var link = links[i];
      if (link.type=="image/x-icon" && link.rel=="shortcut icon") {
        docHead.removeChild(link);
        break; // Assuming only one match at most.
      }
    }
    docHead.appendChild(createFavIconElement(iconURL));
}

function runTests() {
    if (window.testRunner) {
        testRunner.dumpAsText();
        if (testRunner.dumpIconChanges)
            testRunner.dumpIconChanges();
    }

    iconURL = document.getElementsByTagName("head")[0].getElementsByTagName("link")[0].href;
    debugOutput ('Original iconURL is: ' + iconURL);
    newURL = 'http://test.com/newfavicon.ico';
    debugOutput ('Setting new icon URL to: ' + newURL);
    setFavIcon(newURL);
    iconURL = document.getElementsByTagName("head")[0].getElementsByTagName("link")[0].href;

    debugOutput ('New iconURL is: ' + iconURL);

    // check that the URL list in the document is as we expect
    var expectedURLs = "http://test.com/newfavicon.ico";
    var iconURLs = internals.shortcutIconURLs(document);
    if (expectedURLs == iconURLs[0])
        debugOutput('PASS - URL list matches expected');
    else
        debugOutput('FAIL - URL list does not match expected');

    // Add some more fav icons to verify that didChangeIcons gets called.
    var docHead = getHeadElement();
    docHead.insertBefore(createFavIconElement("http://example.org/icon1.ico"), docHead.firstChild);
    docHead.appendChild(createFavIconElement("http://example.org/icon2.ico"));
    if (window.testRunner)
        testRunner.notifyDone();
}

</script>
</head>
<body onload="setTimeout(runTests, 0);">
<div id='debugDiv'>
</div>
</body>
</html>
